<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Configs;
use think\Controller;
use think\Request;

class Webconfig extends AdminBase
{
    private static $ConfigModel = null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (self::$ConfigModel==null){
            self::$ConfigModel=new Configs();
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $webConfig = self::$ConfigModel->where('config_name','webconfig')->field('config_value')->find();
        $this->assign('configData',json_decode($webConfig['config_value'],true));
       return $this->fetch();
    }

    /**
     * 系统配置信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function adminconfig(){
        $webConfig = self::$ConfigModel->where('config_name','adminconfig')->field('config_value')->find();
        $this->assign('configData',json_decode($webConfig['config_value'],true));
        return $this->fetch();
    }

    /**
     * 配置项更改添加
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function webConfigAdd(){
        if (\request()->isAjax()){
            $post = input('post.');
            if (!isset($post['name'])&&empty($post['name'])){
                return $this->resMessage(0,'缺少参数');
            };
            $ConfigData = self::$ConfigModel->where('config_name',$post['name'])->find();
            $value = [
                'title'=>$post['title'],
                'keywords'=>$post['keywords'],
                'describe'=>$post['describe'],
                'icp'=>$post['icp'],
                'btm'=>$post['btm'],
                'ico'=>$post['ico'],
            ];
            $data=[
                'config_name'=>$post['name'],
                'config_value'=>json_encode($value,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)
            ];
            if ($ConfigData!=null){
                $res =self::$ConfigModel->save($data,['config_name'=>$post['name']]);
            }else{
                $res =self::$ConfigModel->save($data);
            }
            if ($res){
                return $this->resMessage(1,'操作成功');
            }else{
                return $this->resMessage(0,'操作失败');
            }

        }
    }


    public function uploadImg(){
        $file = \request()->file('file');
        $path =ROOT_PATH . 'public';
        return $this->upload($file,$path);
    }


}
